# coding:utf-8
import json, datetime, difflib, time
from MUser import User, db, Md5
from sqlalchemy import not_, and_, or_
import httpApi.apiBussiness

def getNameById(id):
    query = User.query.filter(User.id == id).first()
    if query:
        return query.userName

def getUserInfoById(id):
    query = User.query.filter(User.id == id).first()
    if query:
        c = query.__dict__
        del (c['_sa_instance_state'])
        del (c['passWord'])
        return c

def allUser():
    query = db.session().query(User)
    query = query.filter(User.isShow == 0)
    list = []
    for d in query.all():
        list.append({'userName':d.userName,'id':d.id})
    return list

def deleteUser(id):
    query = User.query.filter(User.id == id).first()
    if query:
        query.isShow = 1
        db.session.commit()
        return query.id


def register(content):
    """新建用户"""
    query = User.query.filter(User.userName == content['userName']).first()
    if query:
        return False
    id= User(userName=content['userName'], passWord=content['passWord'], project_id=content['select'],
                    role=content['role']).add()
    return id


def updateUserInfo(content):
    """修改用户信息"""
    query = User.query.filter(User.id == content['id']).first()
    if query:
        query.project_id = content['select']
        query.role = content['role']
        query.userName = content['userName']
        if content['passWord']!="":
            query.passWord = Md5.md5_string(content['passWord'])
        db.session.commit()
        return query.id



def login(content):
    """登录成功后，token也刷新"""
    query = User.query.filter(and_(User.userName == content['userName'],
                                   User.passWord == Md5.md5_string(content['passWord']),
                                   User.isShow == 0
                                   )).first()
    if query:
        #query.token = Md5.md5_string("%s" % int(time.time()))
        #query.last_time = str(datetime.datetime.now())
        c = query.__dict__
        del (c['_sa_instance_state'])
        del (c['passWord'])
        return c


def logout(token, openId):
    """注销时，token刷新"""
    query = User.query.filter(
        and_(User.token == token, User.openId == openId)).first()
    if query:
        query.token = Md5.md5_string("%s" % int(time.time()))
        query.last_time = str(datetime.datetime.now())
        db.session.commit()
        return query.token


def getUserId(openId, token,t=0):
    """根据openId和token获取用户id"""
    query = User.query.filter(
        and_(User.token == openId,
             User.openId == token,
            User.isShow == 0)).first()
    if query:
        if t == 0:
            return query.id
        else:
            return query.project_id


def userList(page, limit):
    query = db.session().query(User)
    count = query.count()
    data = query.paginate(int(page), int(limit), False)
    list = []
    for d in data.items:
        c = d.__dict__
        del (c['_sa_instance_state'])
        del (c['openId'])
        del (c['token'])
        del (c['passWord'])
        data = httpApi.apiBussiness.project_srh_name(c['project_id'])
        str = ''
        for d in data:
            str = d+"," + str
        c['project_name'] = str
        list.append(c)
    return list, count
